package ctrl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import service.Impl.IBookServiceImpl;
import service.Impl.IOrderLineServiceImpl;
import service.Impl.IOrderServiceImpl;
import service.Impl.IOrderStateServiceImpl;
import vo.Book;
import vo.Order;
import vo.OrderLine;
import vo.OrderState;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet(name = "ClerkServlet")
public class ClerkServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        IBookServiceImpl bookService = new IBookServiceImpl();
        IOrderStateServiceImpl orderStateService = new IOrderStateServiceImpl();
        IOrderLineServiceImpl orderLineService = new IOrderLineServiceImpl();
        IOrderServiceImpl orderService = new IOrderServiceImpl();

        List<Book> books = bookService.getAllBook();
        JSONArray bookJA = new JSONArray();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for(Book b : books){
            JSONObject jo = new JSONObject();
            jo.put("id", b.getId());
            jo.put("name", b.getName());
            jo.put("briefIntro", b.getBriefIntro());
            jo.put("author", b.getAuthor());
            jo.put("press", b.getPress());
            Date date = b.getPublication();
            String dateStr = format.format(date);
            jo.put("publication", dateStr);
            jo.put("price", b.getPrice());
            jo.put("type", b.getType());
            jo.put("imgUrl", b.getImgUrl());
            jo.put("inventory", b.getInventory());
            bookJA.add(jo);
        }

        List<OrderState> orderStates = orderStateService.getAllOrderState();
        JSONArray orderJA = new JSONArray();
//        JSONArray orderDetailJA = new JSONArray();
        for(OrderState o: orderStates){
            JSONObject orderJO = new JSONObject();//包含一个订单的简略信息
            Order order = orderService.getOrderById(o.getOrderId());
            orderJO.put("id", order.getId());
            orderJO.put("userId", order.getUserId());
            orderJO.put("totalPrice", order.getTotalPrice());
            orderJO.put("state", o.getState());
            orderJA.add(orderJO);

//            JSONArray orderDetail = new JSONArray();//包含一个订单的详细信息
//            orderDetail.add(orderJO);//将外层的包含四个简略信息的jsonObject添加到json数组orderDetail中
//            List<OrderLine> orderLines = orderLineService.selectOrderLineByOrderId(o.getOrderId());
//            for(OrderLine ol: orderLines){
//                JSONObject olJO = new JSONObject();
//                olJO.put("quanlity", ol.getQuanlity());
//                Book book = bookService.selectBookById(ol.getBookId());
//                olJO.put("id", book.getId());
//                olJO.put("name", book.getName());
//                olJO.put("briefIntro", book.getBriefIntro());
//                olJO.put("author", book.getAuthor());
//                olJO.put("press", book.getPrice());
//                olJO.put("publication", book.getPublication());
//                olJO.put("price", book.getPrice());
//                olJO.put("type", book.getType());
//                olJO.put("imgUrl", book.getImgUrl());
//                olJO.put("inventory", book.getInventory());
//                orderDetail.add(olJO);
//            }
//            orderDetailJA.add(orderDetail);
        }


        JSONObject number = new JSONObject();
        number.put("bookNum", books.size());
        number.put("orderNum", orderStates.size());

        JSONObject result = new JSONObject();
        result.put("number", number);
        result.put("books", bookJA);
        result.put("orders", orderJA);

        response.getWriter().write(String.valueOf(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
